package com.mc.web;

import org.apache.log4j.Logger;

import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.mc.metaSearch.SearchBBS;
import com.mc.metaSearch.SearchBlog;
import com.mc.metaSearch.SearchNews;
import com.mc.model.Path;
import com.mc.utils.PathUtil;
import com.mc.utils.metaSearch;

@Controller
public class metaController {
	/**
	 * Logger for this class
	 */
	private static final Logger logger = Logger.getLogger(metaController.class);
	private static final String RETITLE  = "西华大学数学与计算机学院";
    private Map<String,List<metaSearch>> subBloglist1= new HashMap<String,List<metaSearch>>();
	private Path path = null;
	/**
	 *  
	 * @param currentPage 当前页
	 * @param modelMap
	 * @return 检索网际数计的博客信息
	 */
	@RequestMapping("/view/Intelligent_semantic/Blog/{currentPage}")
	public String Distribution(@PathVariable("currentPage") String currentPage,ModelMap modelMap) {
		ModelAndView mav = new ModelAndView();
		SearchBlog blog = new SearchBlog();
		List<metaSearch> blogList  = null;
		if(subBloglist1.get("blog")!=null)
		{
			blogList = subBloglist1.get("blog");
		}else{
			String[] blogR;
			Map<Integer, String> blogUrls = null;
			Map<Integer, String> blogAbstracts = null;
			Map<Integer, String> blogTitles = null;
			try {
				blogR = blog.getContent(RETITLE);
				blogUrls = blog.getURL(blogR);
				blogAbstracts = blog.getAbstract(blogR);
				blogTitles = blog.getTitle(blogR);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

			 blogList = new LinkedList<metaSearch>();
			Object key = null;
			Iterator blogIt = blogUrls.keySet().iterator();
			while (blogIt.hasNext()) {
				metaSearch meta = new metaSearch();
				key = blogIt.next();
				meta.setUrl(blogUrls.get(key));
				meta.setTitle(blogTitles.get(key));
				meta.setAbstracts(blogAbstracts.get(key));
				blogList.add(meta);
			}
			subBloglist1.put("blog", blogList);
		}
		System.out.println("success");
		int pageCount = getPageCount(blogList.size());
        List<metaSearch> subBloglist = this.fenye(blogList, new Integer(currentPage));
       // System.out.println("pageCount----" + pageCount);
        modelMap.addAttribute("pageCount", pageCount);
        modelMap.addAttribute("blogList", subBloglist);
		// mav.addObject("menu1","back-navi" );
		// mav.addObject("menu2","Intelligent_semantic");
		path = PathUtil.setPathParams(new String[]{"PackageName:Intelligent","ViewName:Intelligent_semantic_blog","ViewTitle:网际数计","smenu:intelligent","mmenu:Intelligent_semantic_blog"});				
		return PathUtil.returnStr(path, modelMap);
	}
/**
 * 
 * @param currentPage 当前页
 * @param modelMap
 * @return 关于西华大学数学与计算机学院的论坛
 */
	@RequestMapping("/view/Intelligent_semantic/Bbs/{currentPage}")
	public String getBbs(@PathVariable("currentPage") String currentPage,ModelMap modelMap) {
		ModelAndView mav = new ModelAndView();
		List<metaSearch> bbsList  = null;
		logger.info("bbssss");
        if(subBloglist1.get("bbs")!=null)
        {
        	//logger.info("bbssss");
        	bbsList = subBloglist1.get("bbs");
        	
        }else{//logger.info("1111111111111bbs");
    		SearchBBS bbs = new SearchBBS();
    		String[] bbsR;
    		Map<Integer, String> bbsUrls = null;
    		Map<Integer, String> bbsAbstracts = null;
    		Map<Integer, String> bbsTitles = null;
    		try {
    			bbsR = bbs.getContent(RETITLE);
    			bbsUrls = bbs.getURL(bbsR);
    			bbsAbstracts = bbs.getAbstract(bbsR);
    			bbsTitles = bbs.getTitle(bbsR);
    		} catch (Exception e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		}
    		 bbsList = new LinkedList<metaSearch>();
    		Iterator bbsIt = bbsUrls.keySet().iterator();
    		Object key = null;
    		while (bbsIt.hasNext()) {
    			metaSearch meta = new metaSearch();
    			key = bbsIt.next();
    			meta.setUrl(bbsUrls.get(key));
    			meta.setTitle(bbsTitles.get(key));
    			meta.setAbstracts(bbsAbstracts.get(key));
    			bbsList.add(meta);
    		}
    		subBloglist1.put("bbs",bbsList);
        }
		int pageCount = getPageCount(bbsList.size());
        List<metaSearch> subBbslist = this.fenye(bbsList, new Integer(currentPage));
       // System.out.println("subBbslist.size()----" + subBbslist.size());
       // System.out.println("pageCount----" + pageCount);
        modelMap.addAttribute("bbsList", subBbslist);
		modelMap.addAttribute("pageCount", pageCount);
		 path = PathUtil.setPathParams(new String[]{"PackageName:Intelligent","ViewName:Intelligent_semantic_bbs","ViewTitle:网际数计","smenu:intelligent","mmenu:Intelligent_semantic_bbs"});				
		return PathUtil.returnStr(path, modelMap);
	}
/**
 * 
 * @param currentPage 当前特
 * @param modelMap
 * @return 关于西华大学数学与计算机学院的新闻
 */
	@RequestMapping("/view/Intelligent_semantic/News/{currentPage}")
	public String getNews(@PathVariable("currentPage") String currentPage,ModelMap modelMap) {
	//	System.out.println("currentPage---" + currentPage);
		List<metaSearch> newsList = null;
		logger.info("newws");
		if(subBloglist1.get("news")!=null)
		{
			newsList = subBloglist1.get("news");
		}
		else{
			SearchNews news = new SearchNews();
			ModelAndView mav = new ModelAndView();
			String[] newsR;
			Map<Integer, String> newsUrls = null;
			Map<Integer, String> newsAbstracts = null;
			Map<Integer, String> newsTitles = null;
			try {
				newsR = news.getContent(RETITLE);
				newsUrls = news.getURL(newsR);
				
				newsAbstracts = news.getAbstract(newsR);
				newsTitles = news.getTitle(newsR);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
			newsList = new LinkedList<metaSearch>();
			Iterator newIt = newsUrls.keySet().iterator();
			Object key = null;
			while (newIt.hasNext()) {
				metaSearch meta = new metaSearch();
				key = newIt.next();
				meta.setUrl(newsUrls.get(key));
				meta.setTitle(newsTitles.get(key));
				meta.setAbstracts(newsAbstracts.get(key));
				newsList.add(meta);
			}
			subBloglist1.put("news",newsList);
		}
		int pageCount = getPageCount(newsList.size());
		
        List<metaSearch> subnewslist = this.fenye(newsList, Integer.parseInt(currentPage));
        modelMap.addAttribute("newsList", subnewslist);
        modelMap.addAttribute("pageCount", pageCount +"");
        modelMap.addAttribute("newsList", subnewslist);
		path = PathUtil.setPathParams(new String[]{"PackageName:Intelligent","ViewName:Intelligent_semantic_news","ViewTitle:网际数计","smenu:intelligent","mmenu:Intelligent_semantic_news"});				
		return PathUtil.returnStr(path, modelMap);
	}

	public List<metaSearch> fenye(List<metaSearch> resource, int CurrentPage
			) {
		List<metaSearch> resultData = null;
		if(resource!=null)
		{
			int pageSize = 10;		
			int fromIndex = (CurrentPage - 1) * pageSize;
			int toIndex = fromIndex + pageSize;
			if(toIndex>resource.size())
			{
				resultData = resource.subList(fromIndex, resource.size());
			}else{
				resultData = resource.subList(fromIndex, toIndex);
			}
		}
		return resultData;
	}
	public int getPageCount(int rowCount)
	{
		
		int pageSize = 10;
		int pageCount = 0;
		if (rowCount % pageSize == 0) {
			pageCount = rowCount / pageSize;
		} else {
			pageCount = rowCount / pageSize + 1;
		}
		return pageCount;
	}

}
